package com.gimhon.login.entity;

import com.gimhon.login.constant.ResultCode;
import lombok.Data;

/**
 * 响应报文
 */
@Data
public class Response<T> {
    private T data; // 返回对象
    private String message;// 提示信息
    private String code;// 报文状态码


    public Response(T data) {
        this.code = ResultCode.SUCCESS.val();
        this.message = ResultCode.SUCCESS.msg();
        this.data = data;
    }

    public Response(String message) {
        this.code = ResultCode.SYS_ERROR.val();
        this.message = message;
    }

    public Response(ResultCode resultCode) {
        this.code = resultCode.val();
        this.message = resultCode.msg();
    }

    public Response(T data, String message, String code) {
        this.data = data;
        this.message = message;
        this.code = code;
    }

    /**
     * 请求成功
     *
     * @param data 成功的反回结果
     * @param <T>  结果类型
     * @return 报文对象
     */
    public static <T> Response success(T data) {
        return new Response<T>(data);
    }

    /**
     * 请求失败
     *
     * @param resultCode 错误的枚举对象
     * @return 报文对象
     */
    public static <T> Response error(ResultCode resultCode) {
        return new Response(resultCode);
    }

    /**
     * 创建返回对象
     * @param data 数据对象
     * @param message 提示信息
     * @param code 错误代码
     * @param <T> 数据类型
     * @return 报文对象
     */
    public static <T> Response build(T data, String message, String code) {
        return new Response<T>(data, message, code);
    }

}
